<?php
    /**
     * This file is part of the Intelligencer package.
     * For the full copyright and license, please view the LICENSE
     * file that was distributed with this source code.
     * 
     * @author      drapeko <roman.drapeko@gmail.com>
     * @copyright   Roman Drapeko <roman.drapeko@gmail.com>, 2009 - present
     */

    /**
     * Template Intelligencer is used when you can identify possible locations of the file
     * by the parameters such as the class name 
     * 
     * @author drapeko
     */
    class itTemplateIntelligencer extends itIntelligencer {
        
        protected $templates = array();
        protected $lastAvailableNullPriority = 1000000;
        
        /**
         * Adds new Template to the Intelligencer. You can set the $priority.
         * If the $priority is null the template will be appended to the end
         * 
         * @param itTemplate $template
         * @param integer $priority
         * @return itIntelligencer
         */
        public function addTemplate(itTemplate $template, $priority = null) {
            if (empty($priority)) {
                $priority = $this->lastAvailableNullPriority++;
            }
            
            $this->templates[$priority][] = $template;
            $template->setIntelligencer($this);
            ksort($this->templates);
            return $this;
        }
        
        /**
         * Adds new Template to the Intelligencer. You can set the $priority.
         * If the $priority is null the template will be appended to the end. 
         * Return the template you just added.
         * 
         * @param itTemplate $template
         * @param integer $priority
         * @return itTemplate
         */
        public function addAndCreateTemplate(itTemplate $template, $priority = null) {
            $this->addTemplate($template, $priority);
            return $template;
        }

        /**
         * Ask Intelligencer to load the class according to the template instructions. 
         * This function is widely used by the itManager.
         * @param $className
         * @return boolean
         */
        public function load($className) {
            list($namespace, $class) = itUtil::getClassAndNS($className);
            
            foreach($this->templates as $priority => $templates) {
                foreach($templates as $t) {
                    $pathes = (array)$t->getPath(array($namespace, $class));
                    foreach($pathes as $file) {
                        if (is_readable($file) && is_file($file)) {
                            include_once($file);
                            if (class_exists($className, false) || interface_exists($className, false)) {
                                return true;
                            }
                        }
                    }
                }
            }
            
            return false;
        }

    }

?>